package com.yunsc.shop.controller.order;

import com.yunsc.common.utils.PageResult;
import com.yunsc.common.utils.Result;
import com.yunsc.common.utils.SecurityUtil;
import com.yunsc.common.validator.ValidationGroups;
import com.yunsc.order.dto.OrderPayDTO;
import com.yunsc.order.dto.QueryOrderDTO;
import com.yunsc.order.sevice.YunscOrderService;
import com.yunsc.order.vo.OrderDetailVO;
import com.yunsc.order.vo.TodoCountsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @version: 1.0
 * @Author: adan
 * @description:
 * @date: 2024-05-09 21:59
 */
@Api(tags = "订单管理")
@RestController
@RequestMapping("order")
public class OrderController {

    @Resource
    private YunscOrderService orderService;

    // /order/detail 用户订单详情
    @ApiOperation(value = "用户订单详情", notes = "用户订单详情")
    @RequestMapping("detail")
    public Result<?> queryOrderDetail(Integer orderId) {
        OrderDetailVO orderDetail = orderService.queryOrderDetailByOrderId(orderId);
        Map<String, Object> result = new HashMap<>();
        result.put("order", orderDetail);
        if (new BigDecimal(orderDetail.getPointsMoney()).compareTo(BigDecimal.ZERO) > 0){
            Map<String, Object> points = new HashMap<>();
            points.put("points_name", "积分");
            result.put("setting", points);
        }
        return Result.ok(result);
    }


    // /order/list 用户订单列表
    @ApiOperation(value = "用户订单列表", notes = "用户订单列表")
    @RequestMapping("list")
    public Result<?> queryOrderList(QueryOrderDTO param) {
        Integer currentUserId = SecurityUtil.getCurrentUserIdEx();
        PageResult<OrderDetailVO> pageResult = orderService.queryUserOrderList(param, currentUserId);
        return Result.resultPageList(pageResult);
    }


    // /order/todoCounts 用户订单统计
    @ApiOperation(value = "用户订单统计", notes = "用户订单统计")
    @RequestMapping("todoCounts")
    public Result<?> todoCounts() {
        Integer currentUserId = SecurityUtil.getCurrentUserIdEx();
        TodoCountsVO counts = orderService.queryUserTodoCounts(currentUserId);
        Map<String, Object> result = new HashMap<>();
        result.put("counts", counts);
        return Result.ok(result);
    }

    // /order/cancel 取消订单
    @ApiOperation(value = "取消订单", notes = "取消订单")
    @PostMapping("cancel")
    public Result<?> cancelOrder(@RequestBody @Validated(ValidationGroups.Update.class) OrderPayDTO param) {
        Integer currentUserId = SecurityUtil.getCurrentUserIdEx();
        orderService.cancelOrder(param.getOrderId(), currentUserId);
        return Result.ok("申请取消成功，请耐心等待系统审核。");
    }

    // /order/receipt 确认收货

    @ApiOperation(value = "确认收货", notes = "确认收货")
    @PostMapping("receipt")
    public Result<?> receiptOrder(@RequestBody @Validated(ValidationGroups.Update.class) OrderPayDTO param) {
        Integer currentUserId = SecurityUtil.getCurrentUserIdEx();
        orderService.receiptOrder(param.getOrderId(), currentUserId);
        return Result.ok("确认收货成功。");
    }

}
